/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scenedemo.migrate.migrate_from_classic_ability_fraction;

import com.bytedance.scene.NavigationSceneUtility;
import com.bytedance.scene.Scene;
import com.bytedance.scene.SceneDelegate;
import com.bytedance.scene.interfaces.ChildSceneLifecycleAdapterCallbacks;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.ui.SceneNavigationContainer;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

/**
 * MigrateFromClassicOhosAbilitySamplesAbility.
 *
 * @since 2021-06-17
 */
public class MigrateFromClassicOhosAbilitySamplesAbility extends FractionAbility implements SceneNavigationContainer {

    private SceneDelegate mSceneAbilityDelegate;
    private Fraction mRootFragment;

    @Override
    public int getThemeId() {
        return super.getThemeId();
    }

    @Override
    public NavigationScene getNavigationScene() {

        return this.mSceneAbilityDelegate != null ? mSceneAbilityDelegate.getNavigationScene() : null;
    }

    @Override
    public boolean isVisible() {
        return false;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_migrate_from_classic_ability_fraction);
        initData();
    }

    private void initData() {
        mRootFragment = ClassicFragment.newInstance();
        StackLayout stackLayoutAll = (StackLayout) findComponentById(ResourceTable.Id_classic_fraction_container);
        getFractionManager().startFractionScheduler().add(ResourceTable.Id_classic_fraction_container,
                mRootFragment).submit();
        Button btnMigrate = (Button) findComponentById(ResourceTable.Id_btn_migrate);
        btnMigrate.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showToast("open detail");
                mSceneAbilityDelegate.getNavigationScene().push(DetailScene.class);
            }
        });

        StackLayout stackLayout = (StackLayout) findComponentById(ResourceTable.Id_scene_container);
        mSceneAbilityDelegate = NavigationSceneUtility.setupWithAbility(this, EmptyHolderScene.class)
                .toView(ResourceTable.Id_scene_container).drawWindowBackground(false)
                .fixSceneWindowBackgroundEnabled(true)
                .sceneBackground(Color.WHITE.getValue())
                .supportRestore(false)
                .build();
        mSceneAbilityDelegate.getNavigationScene().registerChildSceneLifecycleCallbacks(
                new ChildSceneLifecycleAdapterCallbacks() {
                    int count = 0;

                    @Override
                    public void onSceneAbilityCreated(Scene scene, Intent intent) {
                        super.onSceneAbilityCreated(scene, intent);
                        count++;
                        if (count > 0) {
                            stackLayoutAll.setVisibility(Component.HIDE);
                            btnMigrate.setVisibility(Component.HIDE);
                            stackLayout.setClickable(true);
                        }
                    }

                    @Override
                    public void onSceneDestroyed(Scene scene) {
                        super.onSceneDestroyed(scene);
                        count--;
                        if (count <= 1) {
                            stackLayoutAll.setVisibility(Component.VISIBLE);
                            btnMigrate.setVisibility(Component.VISIBLE);
                            stackLayout.setClickable(false);
                        }
                    }
            }, false);
    }

    @Override
    protected void onBackPressed() {
        SceneDelegate sceneDelegate = this.mSceneAbilityDelegate;
        if (!sceneDelegate.onBackPressed()) {
            super.onBackPressed();
        }
    }

    /**
     * EmptyHolderScene.
     *
     * @since 2021-06-17
     */
    public static class EmptyHolderScene extends Scene {

        /**
         * EmptyHolderScene
         */
        public EmptyHolderScene() {

        }

        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            return new Component(container.getContext());
        }

        @Override
        public void onViewCreated(Component component, Intent intent) {
            super.onViewCreated(component, intent);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(Color.TRANSPARENT.getValue()));
            getView().setBackground(shapeElement);
        }
    }

    /**
     * ClassicFragment.
     *
     * @since 2021-06-17
     */
    public static class ClassicFragment extends Fraction {
        /**
         * ClassicFragment
         */
        public ClassicFragment() {

        }

        @Override
        protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            return scatter.parse(ResourceTable.Layout_classic_fragment, container, false);

        }

        /**
         * 获取ClassicFragment
         *
         * @return ClassicFragment
         */
        public static ClassicFragment newInstance() {
            return new ClassicFragment();
        }
    }

    /**
     * DetailScene.
     *
     * @since 2021-06-17
     */
    public static class DetailScene extends Scene {
        /**
         * DetailScene
         */
        public DetailScene() {

        }

        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            return scatter.parse(ResourceTable.Layout_detaile_scene, container, false);
        }
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    private void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(this)
                .parse(com.bytedance.scene.ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(com.bytedance.scene.ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }
}
